package com.rz.htk.order.task;

import com.alibaba.fastjson.JSONObject;
import com.rz.htk.order.entity.OrderTrack;
import com.rz.htk.order.service.IOrderTrackService;
import com.rz.htk.order.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DarongTrackGetTask implements PageProcessor {

    private Logger log = LoggerFactory.getLogger("DarongTrackGetTask");
    private SimpleDateFormat dateTimeFormat1 = new SimpleDateFormat("yyyy/MM/dd");
    private SimpleDateFormat dateTimeFormat2 = new SimpleDateFormat("yyyy-MM-dd");

    private String afterCode;
    private IOrderTrackService orderTrackService;
    private IOrderService IOrderService;
    public DarongTrackGetTask(String afterCode, IOrderService IOrderService, IOrderTrackService orderTrackService){
        this.afterCode =afterCode;
        this.orderTrackService = orderTrackService;
        this.IOrderService = IOrderService;
    }

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        Html html =page.getHtml();
        List<String> tds = html.$("td").all();
        if(tds.size() > 0){
            int size = tds.size() / 4;
            boolean finished = false;
            List<OrderTrack> orderTracks = new ArrayList<>();
            Map<String,String> checkKeyMap = new HashMap<>();
            for (int i = 0; i < size; i++) {
                OrderTrack orderTrack = new OrderTrack();
                int k = i*4;
                String scanDateTd = tds.get(k);
                String scanDateTimeTd = tds.get(k+1);
                String scanInfoTd = tds.get(k+2);
                String scanAddrTd = tds.get(k+3);
                String scanDate = getValue(scanDateTd);
                String year = scanDate.substring(0,scanDate.indexOf("/"));
                Integer realYear = Integer.valueOf(year)+1911;
                String realDateStr = realYear+scanDate.substring(scanDate.indexOf("/"));
                String realDateStrInfo = "";
                try {
                    Date realDate = dateTimeFormat1.parse(realDateStr);
                    realDateStrInfo = dateTimeFormat2.format(realDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String scanDateTime = getValue(scanDateTimeTd);
                String scanInfo = getValue(scanInfoTd);
                String scanAddr = getValue(scanAddrTd);
                String key = scanDateTime+scanInfo+scanAddr;
                if(!checkKeyMap.containsKey(key)){
                    orderTrack.setScanDate(realDateStrInfo+" "+scanDateTime);
                    orderTrack.setScanAddr(scanAddr);
                    orderTrack.setScanInfo(scanInfo);
                    orderTrack.setAfterCode(afterCode);
                    orderTracks.add(orderTrack);
                    if(scanInfo.contains("已完成簽收")){
                        finished = true;
                    }
                    checkKeyMap.put(key,key);
                }
            }
            log.info("抓取大荣的afterCode = {}的轨迹数据 {}",afterCode, JSONObject.toJSONString(orderTracks));
            orderTrackService.deleteByAfterCode(afterCode);
            orderTrackService.saveBatch(orderTracks);
            if(finished){
                IOrderService.updateTrackSuccess(afterCode);
            }
        }
    }

    private String getValue(String node){
        int beginIndex= node.indexOf(">");
        int endIndex= node.lastIndexOf("<");
        return node.substring(beginIndex+1,endIndex);
    }

    @Override
    public Site getSite() {
        return site;
    }
}